package com.example.movie.mate.service.impl;

import com.example.movie.mate.dao.cache.IMovieCacheRepository;
import com.example.movie.mate.dao.mapper.HallMapper;
import com.example.movie.mate.dao.mapper.MovieMapper;
import com.example.movie.mate.dao.mapper.PlayScheduleMapper;
import com.example.movie.mate.exception.ServiceException;
import com.example.movie.mate.pojo.entity.Movie;
import com.example.movie.mate.pojo.entity.PlaySchedule;
import com.example.movie.mate.pojo.param.PlayScheduleAddNewParam;
import com.example.movie.mate.pojo.vo.HallVO;
import com.example.movie.mate.pojo.vo.MovieVO;
import com.example.movie.mate.pojo.vo.PlayScheduleVO;
import com.example.movie.mate.service.IPlayScheduleService;
import com.example.movie.mate.web.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 放映计划服务实现类
 */
@Slf4j
@Service
public class PlayScheduleServiceImpl implements IPlayScheduleService {

    @Autowired
    private PlayScheduleMapper playScheduleMapper;
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private HallMapper hallMapper;
    @Autowired
    private IMovieCacheRepository movieCacheRepository;

    @Override
    public void addNew(PlayScheduleAddNewParam param) {
        Long hallId = param.getHallId();
        HallVO hall = hallMapper.selectById(hallId);
        if (hall == null) {
            throw new ServiceException(StateCode.ERROR_NOT_FOUND, "影厅不存在");
        }

        Long movieId = param.getMovieId();
        MovieVO movie = movieMapper.selectById(movieId);
        if (movie == null) {
            throw new ServiceException(StateCode.ERROR_NOT_FOUND, "电影不存在");
        }

        int seatCount = hall.getRowCount() * hall.getColCount();
        PlaySchedule playSchedule = new PlaySchedule();
        BeanUtils.copyProperties(param, playSchedule);
        playSchedule.setMovieName(movie.getName());
        playSchedule.setHallName(hall.getName());
        playSchedule.setSeatCount(seatCount);
        playSchedule.setSeatStock(seatCount);
        playScheduleMapper.insert(playSchedule);

        Movie movieForUpdate = new Movie();
        movieForUpdate.setId(movieId);
        movieForUpdate.setIsOnSchedule(Movie.ScheduleState.ON_SCHEDULE);
        movieForUpdate.setScheduleStartTime(param.getStartTime());
        movieMapper.updateById(movieForUpdate);

        movie.setIsOnSchedule(Movie.ScheduleState.ON_SCHEDULE);
        movie.setScheduleStartTime(param.getStartTime());
        movieCacheRepository.save(movie);
        movieCacheRepository.removeNotOnScheduleListItem(movie);
        movieCacheRepository.removeNotOnScheduleListItem(movie);
        movieCacheRepository.saveOnScheduleListItem(movie);
    }

    @Override
    public List<PlayScheduleVO> selectByMovieIdAndDate(Long movieId, LocalDate date) {
        return playScheduleMapper.selectByMovieIdAndDate(movieId, date);
    }

    @Override
    public List<PlayScheduleVO> selectByHallIdAndDate(Long hallId, LocalDate date) {
        return playScheduleMapper.selectByHallIdAndDate(hallId, date);
    }
}